package com.slicktechnologies.client.views.settings.company;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.TextBox;
import com.simplesoftwares.client.library.FieldType;
import com.simplesoftwares.client.library.FlexForm.FormStyle;
import com.simplesoftwares.client.library.FormField;
import com.simplesoftwares.client.library.FormFieldBuilder;
import com.simplesoftwares.client.library.appskeleton.AppMemory;
import com.simplesoftwares.client.library.appskeleton.ScreeenState;
import com.simplesoftwares.client.library.appstructure.formscreen.FormScreen;
import com.simplesoftwares.client.library.composite.AddressComposite;
import com.simplesoftwares.client.library.composite.SocialInfoComposites;
import com.simplesoftwares.client.library.composite.UploadComposite;
import com.simplesoftwares.client.library.mywidgets.EmailTextBox;
import com.simplesoftwares.client.library.mywidgets.ObjectListBox;
import com.simplesoftwares.client.library.mywidgets.PhoneNumberBox;
import com.slicktechnologies.client.utility.AppUtility;
import com.slicktechnologies.client.utility.Screen;
import com.slicktechnologies.shared.common.businessunitlayer.Company;
import com.slicktechnologies.shared.common.helperlayer.Config;



public class CompanyForm extends FormScreen<Company>    implements ClickHandler {

//Token to add the varialble declarations
	EmailTextBox etbEmail,etbPointOfContactEmail;
  ObjectListBox<Config> lbCompanyType;
  AddressComposite addressComposite;
  SocialInfoComposites socialInfoComposite;
  TextBox tbCompanyName,tbWebsite,tbPointOfContactName,tbServiceTaxNo,tbVatTaxNo,tbPANCard,tbLicenseNo;
  UploadComposite uploadComposite;
  PhoneNumberBox pnbLandlinePhone,pnbCellPhoneNo1,pnbCellPhoneNo2,pnbFaxNo,pnbPointOfContactLandline,pnbPointOfContactCell;
  
	
	public  CompanyForm() {
		super();
		createGui();
	}

	public CompanyForm  (String[] processlevel, FormField[][] fields,
			FormStyle formstyle) 
	{
		super(processlevel, fields, formstyle);
		createGui();
		
		
	}

	
	/**
	 * Method template to initialize the declared variables.
	 */
	private void initalizeWidget()
	{
		
etbEmail=new EmailTextBox();
    
etbPointOfContactEmail=new EmailTextBox();
    
lbCompanyType=new ObjectListBox<Config>();
    AppUtility.MakeLiveConfig(lbCompanyType,Screen.COMPANYTYPE);

addressComposite=new AddressComposite();
    
socialInfoComposite=new SocialInfoComposites();
    
tbCompanyName=new TextBox();
    
tbWebsite=new TextBox();
    
tbPointOfContactName=new TextBox();
    
tbServiceTaxNo=new TextBox();
    
tbVatTaxNo=new TextBox();
    
tbPANCard=new TextBox();
    
tbLicenseNo=new TextBox();
    
uploadComposite=new UploadComposite();
    
pnbLandlinePhone=new PhoneNumberBox();
    
pnbCellPhoneNo1=new PhoneNumberBox();
    
pnbCellPhoneNo2=new PhoneNumberBox();
    
pnbFaxNo=new PhoneNumberBox();
    
pnbPointOfContactLandline=new PhoneNumberBox();
    
pnbPointOfContactCell=new PhoneNumberBox();
    
	}
	
	/**
	 * method template to create screen formfields
	 */
	@Override
	public void createScreen() {
		
	    
		initalizeWidget();
		
		//Token to initialize the processlevel menus.
		
		
		
		
		//////////////////////////// Form Field Initialization////////////////////////////////////////////////////
		//Token to initialize formfield
		FormFieldBuilder fbuilder;
fbuilder = new FormFieldBuilder();
FormField fgroupingCompanyInformation=fbuilder.setlabel("Company Information").widgetType(FieldType.Grouping).setMandatory(false).setColSpan(4).build();
fbuilder = new FormFieldBuilder("* Company Name",tbCompanyName);
FormField ftbCompanyName= fbuilder.setMandatory(true).setMandatoryMsg("Company Name is Mandatory").setRowSpan(0).setColSpan(0).build();
fbuilder = new FormFieldBuilder("* Company Type",lbCompanyType);
FormField flbCompanyType= fbuilder.setMandatory(true).setMandatoryMsg("Company Type is Mandatory").setRowSpan(0).setColSpan(0).build();
fbuilder = new FormFieldBuilder("Website",tbWebsite);
FormField ftbWebsite= fbuilder.setMandatory(false).setRowSpan(0).setColSpan(0).build();
fbuilder = new FormFieldBuilder("* Email",etbEmail);
FormField fetbEmail= fbuilder.setMandatory(true).setMandatoryMsg("Email is Mandatory").setRowSpan(0).setColSpan(0).build();
fbuilder = new FormFieldBuilder("Landline Phone",pnbLandlinePhone);
FormField fpnbLandlinePhone= fbuilder.setMandatory(false).setRowSpan(0).setColSpan(0).build();
fbuilder = new FormFieldBuilder("* Cell Phone No 1",pnbCellPhoneNo1);
FormField fpnbCellPhoneNo1= fbuilder.setMandatory(true).setMandatoryMsg("Cell Phone No 1 is Mandatory").setRowSpan(0).setColSpan(0).build();
fbuilder = new FormFieldBuilder("Cell Phone No 2",pnbCellPhoneNo2);
FormField fpnbCellPhoneNo2= fbuilder.setMandatory(false).setRowSpan(0).setColSpan(0).build();
fbuilder = new FormFieldBuilder("Fax No",pnbFaxNo);
FormField fpnbFaxNo= fbuilder.setMandatory(false).setRowSpan(0).setColSpan(0).build();
fbuilder = new FormFieldBuilder();
FormField fgroupingPointOfContactInformation=fbuilder.setlabel("Point Of Contact Information").widgetType(FieldType.Grouping).setMandatory(false).setColSpan(4).build();
fbuilder = new FormFieldBuilder("* Point Of Contact Name",tbPointOfContactName);
FormField ftbPointOfContactName= fbuilder.setMandatory(true).setMandatoryMsg("Point Of Contact Name is Mandatory").setRowSpan(0).setColSpan(0).build();
fbuilder = new FormFieldBuilder("Point Of Contact Landline",pnbPointOfContactLandline);
FormField fpnbPointOfContactLandline= fbuilder.setMandatory(false).setRowSpan(0).setColSpan(0).build();
fbuilder = new FormFieldBuilder("* Point Of Contact Cell",pnbPointOfContactCell);
FormField fpnbPointOfContactCell= fbuilder.setMandatory(true).setMandatoryMsg("Point Of Contact Cell is Mandatory").setRowSpan(0).setColSpan(0).build();
fbuilder = new FormFieldBuilder("Point Of Contact Email",etbPointOfContactEmail);
FormField fetbPointOfContactEmail= fbuilder.setMandatory(false).setRowSpan(0).setColSpan(0).build();
fbuilder = new FormFieldBuilder();
FormField fgroupingTaxInformation=fbuilder.setlabel("Tax Information").widgetType(FieldType.Grouping).setMandatory(false).setColSpan(4).build();
fbuilder = new FormFieldBuilder("Service Tax Nr",tbServiceTaxNo);
FormField ftbServiceTaxNo= fbuilder.setMandatory(false).setRowSpan(0).setColSpan(0).build();
fbuilder = new FormFieldBuilder("VAT Registration Nr",tbVatTaxNo);
FormField ftbVatTaxNo= fbuilder.setMandatory(false).setRowSpan(0).setColSpan(0).build();
fbuilder = new FormFieldBuilder("PAN Card",tbPANCard);
FormField ftbPANCard= fbuilder.setMandatory(false).setRowSpan(0).setColSpan(0).build();
fbuilder = new FormFieldBuilder("License Nr",tbLicenseNo);
FormField ftbLicenseNo= fbuilder.setMandatory(false).setRowSpan(0).setColSpan(0).build();
fbuilder = new FormFieldBuilder();
FormField fgroupingSocialInformation=fbuilder.setlabel("Social Information").widgetType(FieldType.Grouping).setMandatory(false).setColSpan(4).build();
fbuilder = new FormFieldBuilder("",socialInfoComposite);
FormField fsocialInfoComposite= fbuilder.setMandatory(false).setRowSpan(0).setColSpan(4).build();
fbuilder = new FormFieldBuilder();
FormField fgroupingAddressInformation=fbuilder.setlabel("Address Information").widgetType(FieldType.Grouping).setMandatory(false).setColSpan(4).build();
fbuilder = new FormFieldBuilder("",addressComposite);
FormField faddressComposite= fbuilder.setMandatory(false).setRowSpan(0).setColSpan(4).build();
fbuilder = new FormFieldBuilder();
FormField fgroupingUploadLogo=fbuilder.setlabel("Upload Logo").widgetType(FieldType.Grouping).setMandatory(false).setColSpan(4).build();
fbuilder = new FormFieldBuilder("",uploadComposite);
FormField fuploadComposite= fbuilder.setMandatory(false).setRowSpan(0).setColSpan(0).build();

		
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		
////////////////////////////////Lay Out Making Code///////////////////////////////////////////////////////////////
		
		
		FormField[][] formfield = {   {fgroupingCompanyInformation},
  {ftbCompanyName,flbCompanyType,ftbWebsite,fetbEmail},
  {fpnbLandlinePhone,fpnbCellPhoneNo1,fpnbCellPhoneNo2,fpnbFaxNo},
  {fgroupingPointOfContactInformation},
  {ftbPointOfContactName,fpnbPointOfContactLandline,fpnbPointOfContactCell,fetbPointOfContactEmail},
  {fgroupingTaxInformation},
  {ftbServiceTaxNo,ftbVatTaxNo,ftbPANCard,ftbLicenseNo},
  {fgroupingSocialInformation},
  {fsocialInfoComposite},
  {fgroupingAddressInformation},
  {faddressComposite},
  {fgroupingUploadLogo},
  {fuploadComposite},
  };

		this.fields=formfield;
					   
   }

	/**
	 * method template to update the model with token entity name
	 */
	@Override
	public void updateModel(Company model) 
	{
		
		if(tbCompanyName.getValue()!=null)
  model.setBusinessUnitName(tbCompanyName.getValue());
if(lbCompanyType.getValue()!=null)
  model.setCompanyType(lbCompanyType.getValue());
if(tbWebsite.getValue()!=null)
  model.setWebsite(tbWebsite.getValue());
if(etbEmail.getValue()!=null)
  model.setEmail(etbEmail.getValue());
if(pnbLandlinePhone.getValue()!=null)
  model.setLandline(pnbLandlinePhone.getValue());
if(pnbCellPhoneNo1.getValue()!=null)
  model.setCellNumber1(pnbCellPhoneNo1.getValue());
if(pnbCellPhoneNo2.getValue()!=null)
  model.setCellNumber2(pnbCellPhoneNo2.getValue());
if(pnbFaxNo.getValue()!=null)
  model.setFaxNumber(pnbFaxNo.getValue());
if(tbPointOfContactName.getValue()!=null)
  model.setPocName(tbPointOfContactName.getValue());
if(pnbPointOfContactLandline.getValue()!=null)
  model.setPocLandline(pnbPointOfContactLandline.getValue());
if(pnbPointOfContactCell.getValue()!=null)
  model.setPocCell(pnbPointOfContactCell.getValue());
if(etbPointOfContactEmail.getValue()!=null)
  model.setPocEmail(etbPointOfContactEmail.getValue());
if(tbServiceTaxNo.getValue()!=null)
  model.setServiceTaxNo(tbServiceTaxNo.getValue());
if(tbVatTaxNo.getValue()!=null)
  model.setVatTaxNo(tbVatTaxNo.getValue());
if(tbPANCard.getValue()!=null)
  model.setPanCard(tbPANCard.getValue());
if(tbLicenseNo.getValue()!=null)
  model.setLicense(tbLicenseNo.getValue());
if(socialInfoComposite.getValue()!=null)
  model.setSocialInfo(socialInfoComposite.getValue());
if(addressComposite.getValue()!=null)
  model.setAddress(addressComposite.getValue());

  model.setLogo(uploadComposite.getValue());

		
	}

	/**
	 * method template to update the view with token entity name
	 */
	@Override
	public void updateView(Company view) 
	{
		
		
		if(view.getBusinessUnitName()!=null)
  tbCompanyName.setValue(view.getBusinessUnitName());
if(view.getCompanyType()!=null)
  lbCompanyType.setValue(view.getCompanyType());
if(view.getWebsite()!=null)
  tbWebsite.setValue(view.getWebsite());
if(view.getEmail()!=null)
  etbEmail.setValue(view.getEmail());
if(view.getLandline()!=null)
  pnbLandlinePhone.setValue(view.getLandline());
if(view.getCellNumber1()!=null)
  pnbCellPhoneNo1.setValue(view.getCellNumber1());
if(view.getCellNumber2()!=null)
  pnbCellPhoneNo2.setValue(view.getCellNumber2());
if(view.getFaxNumber()!=null)
  pnbFaxNo.setValue(view.getFaxNumber());
if(view.getPocName()!=null)
  tbPointOfContactName.setValue(view.getPocName());
if(view.getPocLandline()!=null)
  pnbPointOfContactLandline.setValue(view.getPocLandline());
if(view.getPocCell()!=null)
  pnbPointOfContactCell.setValue(view.getPocCell());
if(view.getPocEmail()!=null)
  etbPointOfContactEmail.setValue(view.getPocEmail());
if(view.getServiceTaxNo()!=null)
  tbServiceTaxNo.setValue(view.getServiceTaxNo());
if(view.getVatTaxNo()!=null)
  tbVatTaxNo.setValue(view.getVatTaxNo());
if(view.getPanCard()!=null)
  tbPANCard.setValue(view.getPanCard());
if(view.getLicense()!=null)
  tbLicenseNo.setValue(view.getLicense());
if(view.getSocialInfo()!=null)
  socialInfoComposite.setValue(view.getSocialInfo());
if(view.getAddress()!=null)
  addressComposite.setValue(view.getAddress());
if(view.getLogo()!=null)
  uploadComposite.setValue(view.getLogo());

	}

	// Hand written code shift in presenter
	
	
	/**
	 * Toggles the app header bar menus as per screen state
	 */
	
	public void toggleAppHeaderBarMenu() {
		if(AppMemory.getAppMemory().currentState==ScreeenState.NEW)
		{
			InlineLabel[] menus=AppMemory.getAppMemory().skeleton.getMenuLabels();
			for(int k=0;k<menus.length;k++)
			{
				String text=menus[k].getText();
				if(text.contains("Save")||text.contains("Discard"))
				{
					menus[k].setVisible(true); 
				}
				else
					menus[k].setVisible(false);  		   
				
			}
		}
		
		else if(AppMemory.getAppMemory().currentState==ScreeenState.EDIT)
		{
			InlineLabel[] menus=AppMemory.getAppMemory().skeleton.getMenuLabels();
			for(int k=0;k<menus.length;k++)
			{
				String text=menus[k].getText();
				if(text.contains("Save")||text.contains("Discard"))
				{
					menus[k].setVisible(true); 
				}
				else
					menus[k].setVisible(false);  		   
			}
		}
		
		else if(AppMemory.getAppMemory().currentState==ScreeenState.VIEW)
		{
			InlineLabel[] menus=AppMemory.getAppMemory().skeleton.getMenuLabels();
			for(int k=0;k<menus.length;k++)
			{
				String text=menus[k].getText();
				if(text.contains("Edit")||text.contains("Discard"))
				{
					menus[k].setVisible(true); 
				}
				else
					menus[k].setVisible(false);  		   
			}
		}
		
	}
	
	/**
	 * sets the id textbox with the passed count value. 
	 */
	@Override
	public void setCount(int count)
	{
	
	}

	
	@Override
	public void onClick(ClickEvent event) {
		// TODO Auto-generated method stub
		
	}

}
